import { selectStoreAreasByName, queryTakeUserList, queryAreaCode } from './service.js';


const kindOptions = [
  { label: '发货仓', value: '1' },
  { label: '销退仓', value: '2' },
  { label: '次品仓', value: '3' },
]

const cityOptions1 = [
  { label: '南通', value: 'NT' }
]

const cityOptions2 = [
  { label: '常熟', value: 'CS' },
  { label: '织里', value: 'ZL' }
]

const hasRemarkOptions = [
  { label: '是', value: '1' },
  { label: '否', value: '0' }
]

const autoPushOptions = [
  { label: '已开启', value: '1' },
  { label: '已关闭', value: '0' }
]

//表单配置项
const formItemData = {
  'merchantAreaCode': {
    itemConfig: {
      name: 'merchantAreaCode'
    },
    elConfig: {
      formType: 'Select',
      placeholder: '城市',
      options: APP_TITLE == '找家纺' ? cityOptions1 : cityOptions2
    }
  },
  'storeArea': {
    itemConfig: {
      name: 'storeArea'
    },
    elConfig: {
      formType: 'SearchSelect',
      placeholder: '门店区域',
      fetchOptions: fetchStoreArea,
    }
  },
  'shopCode': {
    itemConfig: {
      name: 'shopCode'
    },
    elConfig: {
      formType: 'Input',
      placeholder: '档口号',
    }
  },
  'shopAddress': {
    itemConfig: {
      name: 'shopAddress'
    },
    elConfig: {
      formType: 'Input',
      placeholder: '门店地址',
    }
  },
  'storeIds': {
    itemConfig: {
      name: 'storeIds'
    },
    elConfig: {
      formType: 'SearchSupplier',
      placeholder: '供应商',
    }
  },
  'shopName': {
    itemConfig: {
      name: 'shopName'
    },
    elConfig: {
      formType: 'Input',
      placeholder: '门店名称',
    }
  },
  'hasRemark': {
    itemConfig: {
      name: 'hasRemark'
    },
    elConfig: {
      formType: 'Select',
      placeholder: '是否有门店备注',
      options: hasRemarkOptions
    }
  },
  'shopRemark': {
    itemConfig: {
      name: 'shopRemark'
    },
    elConfig: {
      formType: 'Input',
      placeholder: '门店备注',
      options: kindOptions
    }
  },
  'takeUserId': {
    itemConfig: {
      name: 'takeUserId'
    },
    elConfig: {
      formType: 'SearchSelect',
      placeholder: '拿货员',
      fetchOptions: fetchTakerList,
    }
  },

  'refundUserId': {
    itemConfig: {
      name: 'refundUserId'
    },
    elConfig: {
      formType: 'SearchSelect',
      placeholder: '退货员',
      fetchOptions: fetchRefunerList,
    }
  },
  'storeAreaId': {
    itemConfig: {
      name: 'storeAreaId'
    },
    elConfig: {
      formType: 'SearchSelect',
      placeholder: '区域编码',
      fetchOptions: fetchAreaCode,
    }
  },
  'autoPush': {
    itemConfig: {
      name: 'autoPush'
    },
    elConfig: {
      formType: 'Select',
      placeholder: '自动推送打印',
      options: autoPushOptions
    }
  },
  'offlinePaySwitch': {
    itemConfig: {
      name: 'offlinePaySwitch'
    },
    elConfig: {
      formType: 'Select',
      placeholder: '是否开启线下支付',
      options: hasRemarkOptions
    }
  },
  'unpickDisplay': {
    itemConfig: {
      name: 'unpickDisplay'
    },
    elConfig: {
      formType: 'Select',
      placeholder: '待拿货数据同步',
      options: autoPushOptions
    }
  },
}

//获取区域编码
function fetchAreaCode(areaCode) {
  return new Promise( async (resolve, reject)=>{
    const { data, success } = await queryAreaCode({ areaCode });
    if(success){
      const newArr = [{ label: '空', value: 0 }];
      data.map(item => {
        newArr.push({
          label: item.name,
          value: item.id,
        })
      })
      resolve(newArr);
    }else{
      reject();
    }
  })
}

//拿货员列表
function fetchTakerList(username) {
  return new Promise(async (resolve, reject) => {
    const { data, success } = await queryTakeUserList({
      username,
      current: 1,
      size: 20,
      businessType: 3
    });
    if (success) {
      const newArr = [];
      data.map(item => {
        newArr.push({
          label: item.name ? `${item.name}（${item.account}）` : item.account,
          value: item.id,
        })
      })
      resolve(newArr);
    } else {
      reject();
    }
  })
}

//退货员列表
function fetchRefunerList(username) {
  return new Promise(async (resolve, reject) => {
    const { data, success } = await queryTakeUserList({
      username,
      current: 1,
      size: 20,
      businessType: 4
    });
    if (success) {
      const newArr = [];
      data.map(item => {
        newArr.push({
          label: item.name ? `${item.name}（${item.account}）` : item.account,
          value: item.id,
        })
      })
      resolve(newArr);
    } else {
      reject();
    }
  })
}

//获取门店区域
function fetchStoreArea(storeArea) {
  return new Promise(async (resolve, reject) => {
    const { data, success } = await selectStoreAreasByName({ storeArea });
    if (success) {
      const newArr = [];
      data.map(item => {
        newArr.push({
          label: item,
          value: item,
        })
      })
      resolve(newArr);
    } else {
      reject();
    }
  })
}

//处理表单数据
export const getFormItem = () => {
  const arr = [];
  Object.keys(formItemData).map(v => {
    arr.push(formItemData[v]);
  })
  return arr;
}
